<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>old_db_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_db_old_db_test.js.html">old_db_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.oldDbTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.oldDbTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line19"></a>goog.require(&#39;goog.db&#39;);
<a name="line20"></a>goog.require(&#39;goog.db.Cursor&#39;);
<a name="line21"></a>goog.require(&#39;goog.db.Error&#39;);
<a name="line22"></a>goog.require(&#39;goog.db.IndexedDb&#39;);
<a name="line23"></a>goog.require(&#39;goog.db.KeyRange&#39;);
<a name="line24"></a>goog.require(&#39;goog.db.Transaction&#39;);
<a name="line25"></a>goog.require(&#39;goog.events&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line29"></a>goog.require(&#39;goog.userAgent.product&#39;);
<a name="line30"></a>goog.require(&#39;goog.userAgent.product.isVersion&#39;);
<a name="line31"></a>
<a name="line32"></a>// These tests test the old setVersion API, which is only supported on Chrome
<a name="line33"></a>// through version 24. Once Chrome 24 is no longer supported, they (and
<a name="line34"></a>// goog.db.IndexedDb.setVersion) can be removed.
<a name="line35"></a>var idbSupported = goog.userAgent.product.CHROME &amp;&amp;
<a name="line36"></a>    goog.userAgent.product.isVersion(&#39;22&#39;) &amp;&amp;
<a name="line37"></a>    !goog.userAgent.product.isVersion(&#39;25&#39;);
<a name="line38"></a>var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
<a name="line39"></a>asyncTestCase.stepTimeout = 5000;
<a name="line40"></a>var dbName;
<a name="line41"></a>var dbBaseName = &#39;testDb&#39;;
<a name="line42"></a>var globalDb = null;
<a name="line43"></a>
<a name="line44"></a>// On Chrome 24+, the database reports its default version as 1 as opposed to
<a name="line45"></a>// the empty string (as per the new spec).
<a name="line46"></a>var baseVersion = goog.userAgent.product.isVersion(&#39;24&#39;) ? 1 : &#39;&#39;;
<a name="line47"></a>
<a name="line48"></a>function restartDatabase(opt_db) {
<a name="line49"></a>  if (opt_db &amp;&amp; opt_db.isOpen()) {
<a name="line50"></a>    opt_db.close();
<a name="line51"></a>  }
<a name="line52"></a>  return goog.db.openDatabase(dbName);
<a name="line53"></a>}
<a name="line54"></a>
<a name="line55"></a>function addStore(db) {
<a name="line56"></a>  return db.setVersion(&#39;test&#39;).addCallback(function(tx) {
<a name="line57"></a>    db.createObjectStore(&#39;store&#39;);
<a name="line58"></a>    return restartDatabase(db);
<a name="line59"></a>  });
<a name="line60"></a>}
<a name="line61"></a>
<a name="line62"></a>function addStoreWithIndex(db) {
<a name="line63"></a>  return db.setVersion(&#39;test&#39;).addCallback(function(tx) {
<a name="line64"></a>    var store = db.createObjectStore(&#39;store&#39;, {keyPath: &#39;key&#39;});
<a name="line65"></a>    store.createIndex(&#39;index&#39;, &#39;value&#39;);
<a name="line66"></a>    return restartDatabase(db);
<a name="line67"></a>  });
<a name="line68"></a>}
<a name="line69"></a>
<a name="line70"></a>function populateStore(values, keys, db) {
<a name="line71"></a>  var d = new goog.async.Deferred();
<a name="line72"></a>  var putTx = db.createTransaction(
<a name="line73"></a>      [&#39;store&#39;],
<a name="line74"></a>      goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line75"></a>
<a name="line76"></a>  var store = putTx.objectStore(&#39;store&#39;);
<a name="line77"></a>  for (var i = 0; i &lt; values.length; ++i) {
<a name="line78"></a>    store.put(values[i], keys[i]);
<a name="line79"></a>  }
<a name="line80"></a>
<a name="line81"></a>  goog.events.listen(
<a name="line82"></a>      putTx,
<a name="line83"></a>      goog.db.Transaction.EventTypes.ERROR,
<a name="line84"></a>      failOnErrorEvent);
<a name="line85"></a>
<a name="line86"></a>  goog.events.listen(
<a name="line87"></a>      putTx,
<a name="line88"></a>      goog.db.Transaction.EventTypes.COMPLETE,
<a name="line89"></a>      function() {
<a name="line90"></a>        d.callback(db);
<a name="line91"></a>      });
<a name="line92"></a>
<a name="line93"></a>  return d;
<a name="line94"></a>}
<a name="line95"></a>
<a name="line96"></a>function assertStoreValues(values, db) {
<a name="line97"></a>  var assertStoreTx = db.createTransaction([&#39;store&#39;]);
<a name="line98"></a>  assertStoreTx.objectStore(&#39;store&#39;).getAll().addCallback(function(results) {
<a name="line99"></a>    assertSameElements(values, results);
<a name="line100"></a>    closeAndContinue(db);
<a name="line101"></a>  });
<a name="line102"></a>}
<a name="line103"></a>
<a name="line104"></a>function assertStoreDoesntExist(db) {
<a name="line105"></a>  try {
<a name="line106"></a>    db.createTransaction([&#39;store&#39;]);
<a name="line107"></a>    fail(&#39;Create transaction with a non-existent store should have failed.&#39;);
<a name="line108"></a>  } catch (e) {
<a name="line109"></a>    // expected
<a name="line110"></a>    assertEquals(e.code, goog.db.Error.ErrorCode.NOT_FOUND_ERR);
<a name="line111"></a>    closeAndContinue(db);
<a name="line112"></a>  }
<a name="line113"></a>}
<a name="line114"></a>
<a name="line115"></a>function failOnError(err) {
<a name="line116"></a>  fail(err.message);
<a name="line117"></a>}
<a name="line118"></a>
<a name="line119"></a>function failOnErrorEvent(ev) {
<a name="line120"></a>  fail(ev.target.message);
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>function closeAndContinue(db) {
<a name="line124"></a>  db.close();
<a name="line125"></a>  asyncTestCase.continueTesting();
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>function setUp() {
<a name="line129"></a>  if (!idbSupported) {
<a name="line130"></a>    return;
<a name="line131"></a>  }
<a name="line132"></a>
<a name="line133"></a>  // Always use a clean database by generating a new database name.
<a name="line134"></a>  dbName = dbBaseName + Date.now().toString();
<a name="line135"></a>  globalDb = restartDatabase();
<a name="line136"></a>}
<a name="line137"></a>
<a name="line138"></a>function testDatabaseOpened() {
<a name="line139"></a>  if (!idbSupported) {
<a name="line140"></a>    return;
<a name="line141"></a>  }
<a name="line142"></a>
<a name="line143"></a>  asyncTestCase.waitForAsync(&#39;check database is open&#39;);
<a name="line144"></a>  assertNotNull(globalDb);
<a name="line145"></a>  globalDb.branch().addCallback(function(db) {
<a name="line146"></a>    assertTrue(db.isOpen());
<a name="line147"></a>    closeAndContinue(db);
<a name="line148"></a>  }).addErrback(failOnError);
<a name="line149"></a>}
<a name="line150"></a>
<a name="line151"></a>function testSetVersion() {
<a name="line152"></a>  if (!idbSupported) {
<a name="line153"></a>    return;
<a name="line154"></a>  }
<a name="line155"></a>
<a name="line156"></a>  asyncTestCase.waitForAsync(&#39;set version request&#39;);
<a name="line157"></a>  globalDb.branch().addCallback(function(db) {
<a name="line158"></a>    assertEquals(baseVersion, db.getVersion());
<a name="line159"></a>    return db.setVersion(&#39;version&#39;).addCallback(function(tx) {
<a name="line160"></a>      return restartDatabase(db);
<a name="line161"></a>    });
<a name="line162"></a>  }).addCallback(function(db) {
<a name="line163"></a>    assertEquals(&#39;version&#39;, db.getVersion());
<a name="line164"></a>    closeAndContinue(db);
<a name="line165"></a>  }).addErrback(failOnError);
<a name="line166"></a>}
<a name="line167"></a>
<a name="line168"></a>function testManipulateObjectStores() {
<a name="line169"></a>  if (!idbSupported) {
<a name="line170"></a>    return;
<a name="line171"></a>  }
<a name="line172"></a>
<a name="line173"></a>  asyncTestCase.waitForAsync(&#39;set version and manipulate object stores&#39;);
<a name="line174"></a>  globalDb.branch().addCallback(function(db) {
<a name="line175"></a>    assertEquals(baseVersion, db.getVersion());
<a name="line176"></a>    return db.setVersion(&#39;objstores&#39;).addCallback(function(tx) {
<a name="line177"></a>      db.createObjectStore(&#39;basicStore&#39;);
<a name="line178"></a>      db.createObjectStore(&#39;keyPathStore&#39;, {keyPath: &#39;keyGoesHere&#39;});
<a name="line179"></a>      db.createObjectStore(
<a name="line180"></a>          &#39;autoIncrementStore&#39;,
<a name="line181"></a>          {autoIncrement: true});
<a name="line182"></a>      return restartDatabase(db);
<a name="line183"></a>    });
<a name="line184"></a>  }).addCallback(function(db) {
<a name="line185"></a>    var storeNames = db.getObjectStoreNames();
<a name="line186"></a>    assertEquals(3, storeNames.length);
<a name="line187"></a>    assertTrue(storeNames.contains(&#39;basicStore&#39;));
<a name="line188"></a>    assertTrue(storeNames.contains(&#39;keyPathStore&#39;));
<a name="line189"></a>    assertTrue(storeNames.contains(&#39;autoIncrementStore&#39;));
<a name="line190"></a>    assertEquals(&#39;objstores&#39;, db.getVersion());
<a name="line191"></a>    return db.setVersion(&#39;delstores&#39;).addCallback(function(tx) {
<a name="line192"></a>      db.deleteObjectStore(&#39;basicStore&#39;);
<a name="line193"></a>      return restartDatabase(db);
<a name="line194"></a>    });
<a name="line195"></a>  }).addCallback(function(db) {
<a name="line196"></a>    var storeNames = db.getObjectStoreNames();
<a name="line197"></a>    assertEquals(2, storeNames.length);
<a name="line198"></a>    assertFalse(storeNames.contains(&#39;basicStore&#39;));
<a name="line199"></a>    assertTrue(storeNames.contains(&#39;keyPathStore&#39;));
<a name="line200"></a>    assertTrue(storeNames.contains(&#39;autoIncrementStore&#39;));
<a name="line201"></a>    closeAndContinue(db);
<a name="line202"></a>  }).addErrback(failOnError);
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>function testBadObjectStoreManipulation() {
<a name="line206"></a>  if (!idbSupported) {
<a name="line207"></a>    return;
<a name="line208"></a>  }
<a name="line209"></a>
<a name="line210"></a>  asyncTestCase.waitForAsync(&#39;bad object store manipulation&#39;);
<a name="line211"></a>  var expectedCode = goog.db.Error.ErrorCode.INVALID_STATE_ERR;
<a name="line212"></a>  globalDb.branch().addCallback(function(db) {
<a name="line213"></a>    try {
<a name="line214"></a>      db.createObjectStore(&#39;diediedie&#39;);
<a name="line215"></a>      fail(&#39;Create object store outside transaction should have failed.&#39;);
<a name="line216"></a>    } catch (err) {
<a name="line217"></a>      // expected
<a name="line218"></a>      assertEquals(expectedCode, err.code);
<a name="line219"></a>    }
<a name="line220"></a>  }).addCallback(addStore).addCallback(function(db) {
<a name="line221"></a>    try {
<a name="line222"></a>      db.deleteObjectStore(&#39;store&#39;);
<a name="line223"></a>      fail(&#39;Delete object store outside transaction should have failed.&#39;);
<a name="line224"></a>    } catch (err) {
<a name="line225"></a>      // expected
<a name="line226"></a>      assertEquals(expectedCode, err.code);
<a name="line227"></a>    }
<a name="line228"></a>  }).addCallback(function(db) {
<a name="line229"></a>    return db.setVersion(&#39;delstore&#39;).addCallback(function(tx) {
<a name="line230"></a>      try {
<a name="line231"></a>        db.deleteObjectStore(&#39;diediedie&#39;);
<a name="line232"></a>        fail(&#39;Delete non-existent store should have failed.&#39;);
<a name="line233"></a>      } catch (err) {
<a name="line234"></a>        // expected
<a name="line235"></a>        assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
<a name="line236"></a>      }
<a name="line237"></a>      closeAndContinue(db);
<a name="line238"></a>    });
<a name="line239"></a>  }).addErrback(failOnError);
<a name="line240"></a>}
<a name="line241"></a>
<a name="line242"></a>function testGetNonExistentObjectStore() {
<a name="line243"></a>  if (!idbSupported) {
<a name="line244"></a>    return;
<a name="line245"></a>  }
<a name="line246"></a>
<a name="line247"></a>  asyncTestCase.waitForAsync(&#39;get non-existent object store&#39;);
<a name="line248"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line249"></a>    var tx = db.createTransaction([&#39;store&#39;]);
<a name="line250"></a>    try {
<a name="line251"></a>      tx.objectStore(&#39;diediedie&#39;);
<a name="line252"></a>      fail(&#39;getting non-existent object store should have failed&#39;);
<a name="line253"></a>    } catch (err) {
<a name="line254"></a>      assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
<a name="line255"></a>    }
<a name="line256"></a>    closeAndContinue(db);
<a name="line257"></a>  }).addErrback(failOnError);
<a name="line258"></a>}
<a name="line259"></a>
<a name="line260"></a>function testCreateTransaction() {
<a name="line261"></a>  if (!idbSupported) {
<a name="line262"></a>    return;
<a name="line263"></a>  }
<a name="line264"></a>
<a name="line265"></a>  asyncTestCase.waitForAsync(&#39;create transactions&#39;);
<a name="line266"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line267"></a>    var tx = db.createTransaction([&#39;store&#39;]);
<a name="line268"></a>    assertEquals(
<a name="line269"></a>        &#39;mode not READ_ONLY&#39;,
<a name="line270"></a>        goog.db.Transaction.TransactionMode.READ_ONLY,
<a name="line271"></a>        tx.getMode());
<a name="line272"></a>    tx = db.createTransaction(
<a name="line273"></a>        [&#39;store&#39;],
<a name="line274"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line275"></a>    assertEquals(
<a name="line276"></a>        &#39;mode not READ_WRITE&#39;,
<a name="line277"></a>        goog.db.Transaction.TransactionMode.READ_WRITE,
<a name="line278"></a>        tx.getMode());
<a name="line279"></a>    closeAndContinue(db);
<a name="line280"></a>  }).addErrback(failOnError);
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>function testPutRecord() {
<a name="line284"></a>  if (!idbSupported) {
<a name="line285"></a>    return;
<a name="line286"></a>  }
<a name="line287"></a>
<a name="line288"></a>  asyncTestCase.waitForAsync(&#39;putting record&#39;);
<a name="line289"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line290"></a>    var rw = goog.db.Transaction.TransactionMode.READ_WRITE;
<a name="line291"></a>    var COMPLETE = goog.db.Transaction.EventTypes.COMPLETE;
<a name="line292"></a>    var ERROR = goog.db.Transaction.EventTypes.ERROR;
<a name="line293"></a>
<a name="line294"></a>    function checkForOverwrittenValue() {
<a name="line295"></a>      var checkOverwriteTx = db.createTransaction([&#39;store&#39;]);
<a name="line296"></a>      checkOverwriteTx.objectStore(&#39;store&#39;).get(&#39;putKey&#39;).addCallback(
<a name="line297"></a>          function(result) {
<a name="line298"></a>            // this is guaranteed to run before the COMPLETE event fires on
<a name="line299"></a>            // the transaction
<a name="line300"></a>            assertEquals(&#39;overwritten&#39;, result.key);
<a name="line301"></a>            assertEquals(&#39;value2&#39;, result.value);
<a name="line302"></a>          });
<a name="line303"></a>
<a name="line304"></a>      goog.events.listen(checkOverwriteTx, ERROR, failOnErrorEvent);
<a name="line305"></a>      goog.events.listen(checkOverwriteTx, COMPLETE, function() {
<a name="line306"></a>        closeAndContinue(db);
<a name="line307"></a>      });
<a name="line308"></a>    }
<a name="line309"></a>
<a name="line310"></a>    function overwriteValue() {
<a name="line311"></a>      var overwriteTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line312"></a>      overwriteTx.objectStore(&#39;store&#39;).put(
<a name="line313"></a>          {key: &#39;overwritten&#39;, value: &#39;value2&#39;},
<a name="line314"></a>          &#39;putKey&#39;);
<a name="line315"></a>
<a name="line316"></a>      goog.events.listen(overwriteTx, ERROR, failOnErrorEvent);
<a name="line317"></a>      goog.events.listen(overwriteTx, COMPLETE, checkForOverwrittenValue);
<a name="line318"></a>    }
<a name="line319"></a>
<a name="line320"></a>    function checkForInitialValue() {
<a name="line321"></a>      var checkResultsTx = db.createTransaction([&#39;store&#39;]);
<a name="line322"></a>      checkResultsTx.objectStore(&#39;store&#39;).get(&#39;putKey&#39;).addCallback(
<a name="line323"></a>          function(result) {
<a name="line324"></a>            assertEquals(&#39;initial&#39;, result.key);
<a name="line325"></a>            assertEquals(&#39;value1&#39;, result.value);
<a name="line326"></a>          });
<a name="line327"></a>      goog.events.listen(checkResultsTx, ERROR, failOnErrorEvent);
<a name="line328"></a>      goog.events.listen(checkResultsTx, COMPLETE, overwriteValue);
<a name="line329"></a>    }
<a name="line330"></a>
<a name="line331"></a>    var initialPutTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line332"></a>    initialPutTx.objectStore(&#39;store&#39;).put(
<a name="line333"></a>        {key: &#39;initial&#39;, value: &#39;value1&#39;},
<a name="line334"></a>        &#39;putKey&#39;);
<a name="line335"></a>
<a name="line336"></a>    goog.events.listen(initialPutTx, ERROR, failOnErrorEvent);
<a name="line337"></a>    goog.events.listen(initialPutTx, COMPLETE, checkForInitialValue);
<a name="line338"></a>
<a name="line339"></a>  }).addErrback(failOnError);
<a name="line340"></a>}
<a name="line341"></a>
<a name="line342"></a>function testAddRecord() {
<a name="line343"></a>  if (!idbSupported) {
<a name="line344"></a>    return;
<a name="line345"></a>  }
<a name="line346"></a>
<a name="line347"></a>  asyncTestCase.waitForAsync(&#39;adding record&#39;);
<a name="line348"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line349"></a>    var rw = goog.db.Transaction.TransactionMode.READ_WRITE;
<a name="line350"></a>    var COMPLETE = goog.db.Transaction.EventTypes.COMPLETE;
<a name="line351"></a>    var ERROR = goog.db.Transaction.EventTypes.ERROR;
<a name="line352"></a>
<a name="line353"></a>    var initialAddTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line354"></a>    initialAddTx.objectStore(&#39;store&#39;).add(
<a name="line355"></a>        {key: &#39;hi&#39;, value: &#39;something&#39;},
<a name="line356"></a>        &#39;stuff&#39;);
<a name="line357"></a>
<a name="line358"></a>    goog.events.listen(initialAddTx, ERROR, failOnErrorEvent);
<a name="line359"></a>    goog.events.listen(initialAddTx, COMPLETE, function() {
<a name="line360"></a>      var successfulAddTx = db.createTransaction([&#39;store&#39;]);
<a name="line361"></a>      successfulAddTx.objectStore(&#39;store&#39;).get(&#39;stuff&#39;).addCallback(
<a name="line362"></a>          function(result) {
<a name="line363"></a>            assertEquals(&#39;hi&#39;, result.key);
<a name="line364"></a>            assertEquals(&#39;something&#39;, result.value);
<a name="line365"></a>          });
<a name="line366"></a>
<a name="line367"></a>      goog.events.listen(successfulAddTx, ERROR, failOnErrorEvent);
<a name="line368"></a>      goog.events.listen(successfulAddTx, COMPLETE, function() {
<a name="line369"></a>        var addOverwriteTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line370"></a>        addOverwriteTx.objectStore(&#39;store&#39;).add(
<a name="line371"></a>            {key: &#39;bye&#39;, value: &#39;nothing&#39;},
<a name="line372"></a>            &#39;stuff&#39;).addErrback(
<a name="line373"></a>            function(err) {
<a name="line374"></a>              // expected
<a name="line375"></a>              assertEquals(
<a name="line376"></a>                  goog.db.Error.ErrorCode.CONSTRAINT_ERR,
<a name="line377"></a>                  err.code);
<a name="line378"></a>            });
<a name="line379"></a>
<a name="line380"></a>        goog.events.listen(addOverwriteTx, COMPLETE, function() {
<a name="line381"></a>          fail(&#39;adding existing record should not have succeeded&#39;);
<a name="line382"></a>        });
<a name="line383"></a>        goog.events.listen(addOverwriteTx, ERROR, function(ev) {
<a name="line384"></a>          assertEquals(
<a name="line385"></a>              goog.db.Error.ErrorCode.CONSTRAINT_ERR,
<a name="line386"></a>              ev.target.code);
<a name="line387"></a>          closeAndContinue(db);
<a name="line388"></a>        });
<a name="line389"></a>      });
<a name="line390"></a>    });
<a name="line391"></a>  }).addErrback(failOnError);
<a name="line392"></a>}
<a name="line393"></a>
<a name="line394"></a>function testPutRecordKeyPathStore() {
<a name="line395"></a>  if (!idbSupported) {
<a name="line396"></a>    return;
<a name="line397"></a>  }
<a name="line398"></a>
<a name="line399"></a>  asyncTestCase.waitForAsync(&#39;adding record, key path store&#39;);
<a name="line400"></a>  globalDb.branch().addCallback(function(db) {
<a name="line401"></a>    return db.setVersion(&#39;test&#39;).addCallback(function(tx) {
<a name="line402"></a>      db.createObjectStore(&#39;keyStore&#39;, {keyPath: &#39;key&#39;});
<a name="line403"></a>      return restartDatabase(db);
<a name="line404"></a>    });
<a name="line405"></a>  }).addCallback(function(db) {
<a name="line406"></a>    var COMPLETE = goog.db.Transaction.EventTypes.COMPLETE;
<a name="line407"></a>    var ERROR = goog.db.Transaction.EventTypes.ERROR;
<a name="line408"></a>
<a name="line409"></a>    var putTx = db.createTransaction(
<a name="line410"></a>        [&#39;keyStore&#39;],
<a name="line411"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line412"></a>    putTx.objectStore(&#39;keyStore&#39;).put({key: &#39;hi&#39;, value: &#39;something&#39;});
<a name="line413"></a>
<a name="line414"></a>    goog.events.listen(putTx, ERROR, failOnErrorEvent);
<a name="line415"></a>    goog.events.listen(putTx, COMPLETE, function() {
<a name="line416"></a>      var checkResultsTx = db.createTransaction([&#39;keyStore&#39;]);
<a name="line417"></a>      checkResultsTx.objectStore(&#39;keyStore&#39;).get(&#39;hi&#39;).addCallback(
<a name="line418"></a>          function(result) {
<a name="line419"></a>            assertNotUndefined(result);
<a name="line420"></a>            assertEquals(&#39;hi&#39;, result.key);
<a name="line421"></a>            assertEquals(&#39;something&#39;, result.value);
<a name="line422"></a>          });
<a name="line423"></a>
<a name="line424"></a>      goog.events.listen(checkResultsTx, ERROR, failOnErrorEvent);
<a name="line425"></a>      goog.events.listen(checkResultsTx, COMPLETE, function() {
<a name="line426"></a>        closeAndContinue(db);
<a name="line427"></a>      });
<a name="line428"></a>    });
<a name="line429"></a>  }).addErrback(failOnError);
<a name="line430"></a>}
<a name="line431"></a>
<a name="line432"></a>function testPutBadRecordKeyPathStore() {
<a name="line433"></a>  if (!idbSupported) {
<a name="line434"></a>    return;
<a name="line435"></a>  }
<a name="line436"></a>
<a name="line437"></a>  asyncTestCase.waitForAsync(&#39;adding bad record, key path store&#39;);
<a name="line438"></a>  globalDb.branch().addCallback(function(db) {
<a name="line439"></a>    return db.setVersion(&#39;test&#39;).addCallback(function(tx) {
<a name="line440"></a>      db.createObjectStore(&#39;keyStore&#39;, {keyPath: &#39;key&#39;});
<a name="line441"></a>      return restartDatabase(db);
<a name="line442"></a>    });
<a name="line443"></a>  }).addCallback(function(db) {
<a name="line444"></a>    var badTx = db.createTransaction(
<a name="line445"></a>        [&#39;keyStore&#39;],
<a name="line446"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line447"></a>    badTx.objectStore(&#39;keyStore&#39;).put(
<a name="line448"></a>        {key: &#39;diedie&#39;, value: &#39;anything&#39;},
<a name="line449"></a>        &#39;badKey&#39;).addCallback(function() {
<a name="line450"></a>      fail(&#39;inserting with explicit key should have failed&#39;);
<a name="line451"></a>    }).addErrback(function(err) {
<a name="line452"></a>      // expected
<a name="line453"></a>      assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
<a name="line454"></a>      closeAndContinue(db);
<a name="line455"></a>    });
<a name="line456"></a>  }).addErrback(failOnError);
<a name="line457"></a>}
<a name="line458"></a>
<a name="line459"></a>function testPutRecordAutoIncrementStore() {
<a name="line460"></a>  if (!idbSupported) {
<a name="line461"></a>    return;
<a name="line462"></a>  }
<a name="line463"></a>
<a name="line464"></a>  asyncTestCase.waitForAsync(&#39;adding record, auto increment store&#39;);
<a name="line465"></a>  globalDb.branch().addCallback(function(db) {
<a name="line466"></a>    return db.setVersion(&#39;test&#39;).addCallback(function(tx) {
<a name="line467"></a>      db.createObjectStore(&#39;aiStore&#39;, {autoIncrement: true});
<a name="line468"></a>      return restartDatabase(db);
<a name="line469"></a>    });
<a name="line470"></a>  }).addCallback(function(db) {
<a name="line471"></a>    var tx = db.createTransaction(
<a name="line472"></a>        [&#39;aiStore&#39;],
<a name="line473"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line474"></a>    tx.objectStore(&#39;aiStore&#39;).put(&#39;1&#39;);
<a name="line475"></a>    tx.objectStore(&#39;aiStore&#39;).put(&#39;2&#39;);
<a name="line476"></a>    tx.objectStore(&#39;aiStore&#39;).put(&#39;3&#39;);
<a name="line477"></a>    goog.events.listen(
<a name="line478"></a>        tx,
<a name="line479"></a>        goog.db.Transaction.EventTypes.ERROR,
<a name="line480"></a>        failOnErrorEvent);
<a name="line481"></a>
<a name="line482"></a>    goog.events.listen(tx, goog.db.Transaction.EventTypes.COMPLETE, function() {
<a name="line483"></a>      var tx = db.createTransaction([&#39;aiStore&#39;]);
<a name="line484"></a>      tx.objectStore(&#39;aiStore&#39;).getAll().addCallback(function(results) {
<a name="line485"></a>        assertEquals(3, results.length);
<a name="line486"></a>        // only checking to see if the results are included because the keys
<a name="line487"></a>        // are not specified
<a name="line488"></a>        assertNotEquals(-1, results.indexOf(&#39;1&#39;));
<a name="line489"></a>        assertNotEquals(-1, results.indexOf(&#39;2&#39;));
<a name="line490"></a>        assertNotEquals(-1, results.indexOf(&#39;3&#39;));
<a name="line491"></a>        closeAndContinue(db);
<a name="line492"></a>      });
<a name="line493"></a>    });
<a name="line494"></a>  }).addErrback(failOnError);
<a name="line495"></a>}
<a name="line496"></a>
<a name="line497"></a>function testPutRecordKeyPathAndAutoIncrementStore() {
<a name="line498"></a>  if (!idbSupported) {
<a name="line499"></a>    return;
<a name="line500"></a>  }
<a name="line501"></a>
<a name="line502"></a>  asyncTestCase.waitForAsync(&#39;adding record, key path + auto increment store&#39;);
<a name="line503"></a>  globalDb.branch().addCallback(function(db) {
<a name="line504"></a>    return db.setVersion(&#39;test&#39;).addCallback(function(tx) {
<a name="line505"></a>      db.createObjectStore(&#39;hybridStore&#39;, {
<a name="line506"></a>        keyPath: &#39;key&#39;,
<a name="line507"></a>        autoIncrement: true
<a name="line508"></a>      });
<a name="line509"></a>      return restartDatabase(db);
<a name="line510"></a>    });
<a name="line511"></a>  }).addCallback(function(db) {
<a name="line512"></a>    var tx = db.createTransaction(
<a name="line513"></a>        [&#39;hybridStore&#39;],
<a name="line514"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line515"></a>    tx.objectStore(&#39;hybridStore&#39;).put({value: &#39;whatever&#39;});
<a name="line516"></a>    goog.events.listen(
<a name="line517"></a>        tx,
<a name="line518"></a>        goog.db.Transaction.EventTypes.ERROR,
<a name="line519"></a>        failOnErrorEvent);
<a name="line520"></a>
<a name="line521"></a>    goog.events.listen(tx, goog.db.Transaction.EventTypes.COMPLETE, function() {
<a name="line522"></a>      var tx = db.createTransaction([&#39;hybridStore&#39;]);
<a name="line523"></a>      tx.objectStore(&#39;hybridStore&#39;).getAll().addCallback(function(results) {
<a name="line524"></a>        assertEquals(1, results.length);
<a name="line525"></a>        assertEquals(&#39;whatever&#39;, results[0].value);
<a name="line526"></a>        assertNotUndefined(results[0].key);
<a name="line527"></a>        closeAndContinue(db);
<a name="line528"></a>      });
<a name="line529"></a>    });
<a name="line530"></a>  }).addErrback(failOnError);
<a name="line531"></a>}
<a name="line532"></a>
<a name="line533"></a>function testPutIllegalRecords() {
<a name="line534"></a>  if (!idbSupported) {
<a name="line535"></a>    return;
<a name="line536"></a>  }
<a name="line537"></a>
<a name="line538"></a>  asyncTestCase.waitForAsync(&#39;adding illegal records&#39;);
<a name="line539"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line540"></a>    var tx = db.createTransaction(
<a name="line541"></a>        [&#39;store&#39;],
<a name="line542"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line543"></a>
<a name="line544"></a>    tx.objectStore(&#39;store&#39;).put(&#39;death&#39;, null).addCallback(function() {
<a name="line545"></a>      fail(&#39;putting with null key should have failed&#39;);
<a name="line546"></a>    }).addErrback(function(err) {
<a name="line547"></a>      assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
<a name="line548"></a>    });
<a name="line549"></a>
<a name="line550"></a>    tx.objectStore(&#39;store&#39;).put(&#39;death&#39;, NaN).addCallback(function() {
<a name="line551"></a>      fail(&#39;putting with NaN key should have failed&#39;);
<a name="line552"></a>    }).addErrback(function(err) {
<a name="line553"></a>      assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
<a name="line554"></a>    });
<a name="line555"></a>
<a name="line556"></a>    tx.objectStore(&#39;store&#39;).put(&#39;death&#39;, undefined).addCallback(function() {
<a name="line557"></a>      fail(&#39;putting with undefined key should have failed&#39;);
<a name="line558"></a>    }).addErrback(function(err) {
<a name="line559"></a>      assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
<a name="line560"></a>    });
<a name="line561"></a>
<a name="line562"></a>    closeAndContinue(db);
<a name="line563"></a>  }).addErrback(failOnError);
<a name="line564"></a>}
<a name="line565"></a>
<a name="line566"></a>function testPutIllegalRecordsWithIndex() {
<a name="line567"></a>  if (!idbSupported) {
<a name="line568"></a>    return;
<a name="line569"></a>  }
<a name="line570"></a>
<a name="line571"></a>  asyncTestCase.waitForAsync(&#39;adding illegal records&#39;);
<a name="line572"></a>  globalDb.branch().addCallback(addStoreWithIndex).addCallback(function(db) {
<a name="line573"></a>    var tx = db.createTransaction(
<a name="line574"></a>        [&#39;store&#39;],
<a name="line575"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line576"></a>
<a name="line577"></a>    tx.objectStore(&#39;store&#39;).put({key: &#39;diediedie&#39;, value: null}).
<a name="line578"></a>        addErrback(function(err) {
<a name="line579"></a>          assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
<a name="line580"></a>        });
<a name="line581"></a>
<a name="line582"></a>    tx.objectStore(&#39;store&#39;).put({key: &#39;dietodeath&#39;, value: NaN}).
<a name="line583"></a>        addErrback(function(err) {
<a name="line584"></a>          assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
<a name="line585"></a>        });
<a name="line586"></a>
<a name="line587"></a>    tx.objectStore(&#39;store&#39;).put({key: &#39;dietodeath&#39;, value: undefined}).
<a name="line588"></a>        addErrback(function(err) {
<a name="line589"></a>          assertEquals(goog.db.Error.ErrorCode.DATA_ERR, err.code);
<a name="line590"></a>        });
<a name="line591"></a>
<a name="line592"></a>    closeAndContinue(db);
<a name="line593"></a>  }).addErrback(failOnError);
<a name="line594"></a>}
<a name="line595"></a>
<a name="line596"></a>function testDeleteRecord() {
<a name="line597"></a>  if (!idbSupported) {
<a name="line598"></a>    return;
<a name="line599"></a>  }
<a name="line600"></a>
<a name="line601"></a>  asyncTestCase.waitForAsync(&#39;deleting record&#39;);
<a name="line602"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line603"></a>    var rw = goog.db.Transaction.TransactionMode.READ_WRITE;
<a name="line604"></a>    var COMPLETE = goog.db.Transaction.EventTypes.COMPLETE;
<a name="line605"></a>    var ERROR = goog.db.Transaction.EventTypes.ERROR;
<a name="line606"></a>    var putTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line607"></a>    putTx.objectStore(&#39;store&#39;).put({key: &#39;hi&#39;, value: &#39;something&#39;}, &#39;stuff&#39;);
<a name="line608"></a>
<a name="line609"></a>    goog.events.listen(putTx, ERROR, failOnErrorEvent);
<a name="line610"></a>    goog.events.listen(putTx, COMPLETE, function() {
<a name="line611"></a>      var deleteTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line612"></a>      deleteTx.objectStore(&#39;store&#39;).remove(&#39;stuff&#39;);
<a name="line613"></a>
<a name="line614"></a>      goog.events.listen(deleteTx, ERROR, failOnErrorEvent);
<a name="line615"></a>      goog.events.listen(deleteTx, COMPLETE, function() {
<a name="line616"></a>        var checkResultsTx = db.createTransaction([&#39;store&#39;]);
<a name="line617"></a>        checkResultsTx.objectStore(&#39;store&#39;).get(&#39;stuff&#39;).addCallback(
<a name="line618"></a>            function(result) {
<a name="line619"></a>              assertUndefined(result);
<a name="line620"></a>            });
<a name="line621"></a>
<a name="line622"></a>        goog.events.listen(checkResultsTx, ERROR, failOnErrorEvent);
<a name="line623"></a>        goog.events.listen(checkResultsTx, COMPLETE, function() {
<a name="line624"></a>          closeAndContinue(db);
<a name="line625"></a>        });
<a name="line626"></a>      });
<a name="line627"></a>    });
<a name="line628"></a>  }).addErrback(failOnError);
<a name="line629"></a>}
<a name="line630"></a>
<a name="line631"></a>function testGetAll() {
<a name="line632"></a>  if (!idbSupported) {
<a name="line633"></a>    return;
<a name="line634"></a>  }
<a name="line635"></a>
<a name="line636"></a>  var values = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;];
<a name="line637"></a>  var keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];
<a name="line638"></a>
<a name="line639"></a>  var addData = goog.partial(populateStore, values, keys);
<a name="line640"></a>  var checkStore = goog.partial(assertStoreValues, values);
<a name="line641"></a>
<a name="line642"></a>  asyncTestCase.waitForAsync(&#39;getting all&#39;);
<a name="line643"></a>  globalDb.branch()
<a name="line644"></a>      .addCallbacks(addStore, failOnError)
<a name="line645"></a>      .addCallback(addData)
<a name="line646"></a>      .addCallback(checkStore);
<a name="line647"></a>}
<a name="line648"></a>
<a name="line649"></a>function testCursorGet() {
<a name="line650"></a>  if (!idbSupported) {
<a name="line651"></a>    return;
<a name="line652"></a>  }
<a name="line653"></a>
<a name="line654"></a>  var values = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;];
<a name="line655"></a>  var keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line656"></a>
<a name="line657"></a>  var addData = goog.partial(populateStore, values, keys);
<a name="line658"></a>
<a name="line659"></a>  // Open the cursor over range [&#39;b&#39;, &#39;c&#39;], move in backwards direction.
<a name="line660"></a>  var openCursorAndCheck = function(db) {
<a name="line661"></a>    var cursorTx = db.createTransaction([&#39;store&#39;]);
<a name="line662"></a>    var store = cursorTx.objectStore(&#39;store&#39;);
<a name="line663"></a>    var d = new goog.async.Deferred();
<a name="line664"></a>    var values = [];
<a name="line665"></a>    var keys = [];
<a name="line666"></a>    var cursor = store.openCursor(
<a name="line667"></a>        goog.db.KeyRange.bound(&#39;b&#39;, &#39;c&#39;),
<a name="line668"></a>        goog.db.Cursor.Direction.PREV);
<a name="line669"></a>
<a name="line670"></a>    var key = goog.events.listen(
<a name="line671"></a>        cursor, goog.db.Cursor.EventType.NEW_DATA, function() {
<a name="line672"></a>          values.push(cursor.getValue());
<a name="line673"></a>          keys.push(cursor.getKey());
<a name="line674"></a>          cursor.next();
<a name="line675"></a>        });
<a name="line676"></a>
<a name="line677"></a>    goog.events.listenOnce(cursor, [
<a name="line678"></a>      goog.db.Cursor.EventType.COMPLETE,
<a name="line679"></a>      goog.db.Cursor.EventType.ERROR
<a name="line680"></a>    ], function(evt) {
<a name="line681"></a>      goog.events.unlistenByKey(key);
<a name="line682"></a>      if (evt.type == goog.db.Cursor.EventType.COMPLETE) {
<a name="line683"></a>        d.callback(db);
<a name="line684"></a>      } else {
<a name="line685"></a>        d.errback();
<a name="line686"></a>      }
<a name="line687"></a>    });
<a name="line688"></a>
<a name="line689"></a>    d.addCallback(function(db) {
<a name="line690"></a>      assertArrayEquals([&#39;3&#39;, &#39;2&#39;], values);
<a name="line691"></a>      assertArrayEquals([&#39;c&#39;, &#39;b&#39;], keys);
<a name="line692"></a>      closeAndContinue(db);
<a name="line693"></a>    });
<a name="line694"></a>  };
<a name="line695"></a>
<a name="line696"></a>  asyncTestCase.waitForAsync(&#39;getting range&#39;);
<a name="line697"></a>  globalDb.branch()
<a name="line698"></a>      .addCallbacks(addStore, failOnError)
<a name="line699"></a>      .addCallback(addData)
<a name="line700"></a>      .addCallback(openCursorAndCheck);
<a name="line701"></a>}
<a name="line702"></a>
<a name="line703"></a>function testCursorReplace() {
<a name="line704"></a>  if (!idbSupported) {
<a name="line705"></a>    return;
<a name="line706"></a>  }
<a name="line707"></a>
<a name="line708"></a>  var values = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;];
<a name="line709"></a>  var keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line710"></a>
<a name="line711"></a>  var addData = goog.partial(populateStore, values, keys);
<a name="line712"></a>
<a name="line713"></a>  // Store should contain [&#39;1&#39;, &#39;2&#39;, &#39;5&#39;, &#39;4&#39;] after replacement.
<a name="line714"></a>  var checkStore = goog.partial(assertStoreValues, [&#39;1&#39;, &#39;2&#39;, &#39;5&#39;, &#39;4&#39;]);
<a name="line715"></a>
<a name="line716"></a>  // Use a bounded cursor for (&#39;b&#39;, &#39;c&#39;] to update value &#39;3&#39; -&gt; &#39;5&#39;.
<a name="line717"></a>  var openCursorAndReplace = function(db) {
<a name="line718"></a>    var d = new goog.async.Deferred();
<a name="line719"></a>    var cursorTx = db.createTransaction(
<a name="line720"></a>        [&#39;store&#39;],
<a name="line721"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line722"></a>    var store = cursorTx.objectStore(&#39;store&#39;);
<a name="line723"></a>    var cursor = store.openCursor(goog.db.KeyRange.bound(&#39;b&#39;, &#39;c&#39;, true));
<a name="line724"></a>
<a name="line725"></a>    var key = goog.events.listen(
<a name="line726"></a>        cursor, goog.db.Cursor.EventType.NEW_DATA, function() {
<a name="line727"></a>          assertEquals(&#39;3&#39;, cursor.getValue());
<a name="line728"></a>          cursor.update(&#39;5&#39;).addCallback(function() {
<a name="line729"></a>            cursor.next();
<a name="line730"></a>          }).addErrback(failOnError);
<a name="line731"></a>        });
<a name="line732"></a>
<a name="line733"></a>    goog.events.listenOnce(cursor, [
<a name="line734"></a>      goog.db.Cursor.EventType.COMPLETE,
<a name="line735"></a>      goog.db.Cursor.EventType.ERROR
<a name="line736"></a>    ], function(evt) {
<a name="line737"></a>      goog.events.unlistenByKey(key);
<a name="line738"></a>      if (evt.type == goog.db.Cursor.EventType.COMPLETE) {
<a name="line739"></a>        d.callback(db);
<a name="line740"></a>      } else {
<a name="line741"></a>        d.errback();
<a name="line742"></a>      }
<a name="line743"></a>    });
<a name="line744"></a>    return d;
<a name="line745"></a>  };
<a name="line746"></a>
<a name="line747"></a>  // Setup and execute test case.
<a name="line748"></a>  asyncTestCase.waitForAsync(&#39;replacing value by cursor&#39;);
<a name="line749"></a>  globalDb.branch()
<a name="line750"></a>      .addCallbacks(addStore, failOnError)
<a name="line751"></a>      .addCallback(addData)
<a name="line752"></a>      .addCallback(openCursorAndReplace)
<a name="line753"></a>      .addCallback(checkStore);
<a name="line754"></a>}
<a name="line755"></a>
<a name="line756"></a>function testCursorRemove() {
<a name="line757"></a>  if (!idbSupported) {
<a name="line758"></a>    return;
<a name="line759"></a>  }
<a name="line760"></a>
<a name="line761"></a>  var values = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;];
<a name="line762"></a>  var keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line763"></a>
<a name="line764"></a>  var addData = goog.partial(populateStore, values, keys);
<a name="line765"></a>
<a name="line766"></a>  // Store should contain [&#39;1&#39;, &#39;2&#39;] after removing elements.
<a name="line767"></a>  var checkStore = goog.partial(assertStoreValues, [&#39;1&#39;, &#39;2&#39;]);
<a name="line768"></a>
<a name="line769"></a>  // Use a bounded cursor for (&#39;b&#39;, ...) to remove &#39;3&#39;, &#39;4&#39;.
<a name="line770"></a>  var openCursorAndRemove = function(db) {
<a name="line771"></a>    var d = new goog.async.Deferred();
<a name="line772"></a>    var cursorTx = db.createTransaction(
<a name="line773"></a>        [&#39;store&#39;],
<a name="line774"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line775"></a>    var store = cursorTx.objectStore(&#39;store&#39;);
<a name="line776"></a>    var cursor = store.openCursor(goog.db.KeyRange.lowerBound(&#39;b&#39;, true));
<a name="line777"></a>
<a name="line778"></a>    var key = goog.events.listen(
<a name="line779"></a>        cursor, goog.db.Cursor.EventType.NEW_DATA, function() {
<a name="line780"></a>          cursor.remove(&#39;5&#39;).addCallback(function() {
<a name="line781"></a>            cursor.next();
<a name="line782"></a>          }).addErrback(failOnError);
<a name="line783"></a>        });
<a name="line784"></a>
<a name="line785"></a>    goog.events.listenOnce(cursor, [
<a name="line786"></a>      goog.db.Cursor.EventType.COMPLETE,
<a name="line787"></a>      goog.db.Cursor.EventType.ERROR
<a name="line788"></a>    ], function(evt) {
<a name="line789"></a>      goog.events.unlistenByKey(key);
<a name="line790"></a>      if (evt.type == goog.db.Cursor.EventType.COMPLETE) {
<a name="line791"></a>        d.callback(db);
<a name="line792"></a>      } else {
<a name="line793"></a>        d.errback();
<a name="line794"></a>      }
<a name="line795"></a>    });
<a name="line796"></a>    return d;
<a name="line797"></a>  };
<a name="line798"></a>
<a name="line799"></a>  // Setup and execute test case.
<a name="line800"></a>  asyncTestCase.waitForAsync(&#39;removing value by cursor&#39;);
<a name="line801"></a>  globalDb.branch()
<a name="line802"></a>      .addCallbacks(addStore, failOnError)
<a name="line803"></a>      .addCallback(addData)
<a name="line804"></a>      .addCallback(openCursorAndRemove)
<a name="line805"></a>      .addCallback(checkStore);
<a name="line806"></a>}
<a name="line807"></a>
<a name="line808"></a>function testClear() {
<a name="line809"></a>  if (!idbSupported) {
<a name="line810"></a>    return;
<a name="line811"></a>  }
<a name="line812"></a>
<a name="line813"></a>  asyncTestCase.waitForAsync(&#39;clearing&#39;);
<a name="line814"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line815"></a>    var rw = goog.db.Transaction.TransactionMode.READ_WRITE;
<a name="line816"></a>    var COMPLETE = goog.db.Transaction.EventTypes.COMPLETE;
<a name="line817"></a>    var ERROR = goog.db.Transaction.EventTypes.ERROR;
<a name="line818"></a>
<a name="line819"></a>    var putTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line820"></a>    putTx.objectStore(&#39;store&#39;).put(&#39;1&#39;, &#39;a&#39;);
<a name="line821"></a>    putTx.objectStore(&#39;store&#39;).put(&#39;2&#39;, &#39;b&#39;);
<a name="line822"></a>    putTx.objectStore(&#39;store&#39;).put(&#39;3&#39;, &#39;c&#39;);
<a name="line823"></a>
<a name="line824"></a>    goog.events.listen(putTx, ERROR, failOnErrorEvent);
<a name="line825"></a>    goog.events.listen(putTx, COMPLETE, function() {
<a name="line826"></a>      var clearTx = db.createTransaction([&#39;store&#39;], rw);
<a name="line827"></a>      clearTx.objectStore(&#39;store&#39;).clear();
<a name="line828"></a>
<a name="line829"></a>      goog.events.listen(clearTx, ERROR, failOnErrorEvent);
<a name="line830"></a>      goog.events.listen(clearTx, COMPLETE, function() {
<a name="line831"></a>        var checkResultsTx = db.createTransaction([&#39;store&#39;]);
<a name="line832"></a>        checkResultsTx.objectStore(&#39;store&#39;).getAll().addCallback(
<a name="line833"></a>            function(results) {
<a name="line834"></a>              assertEquals(0, results.length);
<a name="line835"></a>            }).addErrback(failOnError);
<a name="line836"></a>
<a name="line837"></a>        goog.events.listen(checkResultsTx, ERROR, failOnErrorEvent);
<a name="line838"></a>        goog.events.listen(checkResultsTx, COMPLETE, function() {
<a name="line839"></a>          closeAndContinue(db);
<a name="line840"></a>        });
<a name="line841"></a>      });
<a name="line842"></a>    });
<a name="line843"></a>  }).addErrback(failOnError);
<a name="line844"></a>}
<a name="line845"></a>
<a name="line846"></a>function testAbortTransaction() {
<a name="line847"></a>  if (!idbSupported) {
<a name="line848"></a>    return;
<a name="line849"></a>  }
<a name="line850"></a>
<a name="line851"></a>  asyncTestCase.waitForAsync(&#39;abort transaction&#39;);
<a name="line852"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line853"></a>    var abortTx = db.createTransaction(
<a name="line854"></a>        [&#39;store&#39;],
<a name="line855"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line856"></a>    abortTx.objectStore(&#39;store&#39;).put(&#39;data&#39;, &#39;stuff&#39;).addCallback(function() {
<a name="line857"></a>      abortTx.abort();
<a name="line858"></a>    });
<a name="line859"></a>    goog.events.listen(
<a name="line860"></a>        abortTx,
<a name="line861"></a>        goog.db.Transaction.EventTypes.ERROR,
<a name="line862"></a>        failOnErrorEvent);
<a name="line863"></a>
<a name="line864"></a>    goog.events.listen(
<a name="line865"></a>        abortTx,
<a name="line866"></a>        goog.db.Transaction.EventTypes.COMPLETE,
<a name="line867"></a>        function() {
<a name="line868"></a>          fail(&#39;transaction shouldn\&#39;t have completed after being aborted&#39;);
<a name="line869"></a>        });
<a name="line870"></a>
<a name="line871"></a>    goog.events.listen(
<a name="line872"></a>        abortTx,
<a name="line873"></a>        goog.db.Transaction.EventTypes.ABORT,
<a name="line874"></a>        function() {
<a name="line875"></a>          var checkResultsTx = db.createTransaction([&#39;store&#39;]);
<a name="line876"></a>          checkResultsTx.objectStore(&#39;store&#39;).get(&#39;stuff&#39;).addCallback(
<a name="line877"></a>              function(result) {
<a name="line878"></a>                assertUndefined(result);
<a name="line879"></a>              });
<a name="line880"></a>          goog.events.listen(
<a name="line881"></a>              checkResultsTx,
<a name="line882"></a>              goog.db.Transaction.EventTypes.COMPLETE,
<a name="line883"></a>              function() {
<a name="line884"></a>                closeAndContinue(db);
<a name="line885"></a>              });
<a name="line886"></a>        });
<a name="line887"></a>  }).addErrback(failOnError);
<a name="line888"></a>}
<a name="line889"></a>
<a name="line890"></a>function testInactiveTransaction() {
<a name="line891"></a>  if (!idbSupported) {
<a name="line892"></a>    return;
<a name="line893"></a>  }
<a name="line894"></a>
<a name="line895"></a>  asyncTestCase.waitForAsync(&#39;inactive transaction&#39;);
<a name="line896"></a>  globalDb.branch().addCallback(addStoreWithIndex).addCallback(function(db) {
<a name="line897"></a>    var tx = db.createTransaction(
<a name="line898"></a>        [&#39;store&#39;],
<a name="line899"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line900"></a>    var store = tx.objectStore(&#39;store&#39;);
<a name="line901"></a>    var index = store.getIndex(&#39;index&#39;);
<a name="line902"></a>    store.put({key: &#39;something&#39;, value: &#39;anything&#39;});
<a name="line903"></a>    goog.events.listen(tx, goog.db.Transaction.EventTypes.COMPLETE, function() {
<a name="line904"></a>      var expectedCode = goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR;
<a name="line905"></a>      store.put({
<a name="line906"></a>        key: &#39;another&#39;,
<a name="line907"></a>        value: &#39;thing&#39;
<a name="line908"></a>      }).addCallback(function() {
<a name="line909"></a>        fail(&#39;putting with inactive transaction should have failed&#39;);
<a name="line910"></a>      }).addErrback(function(err) {
<a name="line911"></a>        assertEquals(expectedCode, err.code);
<a name="line912"></a>      });
<a name="line913"></a>      store.add({
<a name="line914"></a>        key: &#39;another&#39;,
<a name="line915"></a>        value: &#39;thing&#39;
<a name="line916"></a>      }).addCallback(function() {
<a name="line917"></a>        fail(&#39;adding with inactive transaction should have failed&#39;);
<a name="line918"></a>      }).addErrback(function(err) {
<a name="line919"></a>        assertEquals(expectedCode, err.code);
<a name="line920"></a>      });
<a name="line921"></a>      store.remove(&#39;something&#39;).addCallback(function() {
<a name="line922"></a>        fail(&#39;deleting with inactive transaction should have failed&#39;);
<a name="line923"></a>      }).addErrback(function(err) {
<a name="line924"></a>        assertEquals(expectedCode, err.code);
<a name="line925"></a>      });
<a name="line926"></a>      store.get(&#39;something&#39;).addCallback(function() {
<a name="line927"></a>        fail(&#39;getting with inactive transaction should have failed&#39;);
<a name="line928"></a>      }).addErrback(function(err) {
<a name="line929"></a>        assertEquals(expectedCode, err.code);
<a name="line930"></a>      });
<a name="line931"></a>      store.getAll().addCallback(function() {
<a name="line932"></a>        fail(&#39;getting all with inactive transaction should have failed&#39;);
<a name="line933"></a>      }).addErrback(function(err) {
<a name="line934"></a>        assertEquals(expectedCode, err.code);
<a name="line935"></a>      });
<a name="line936"></a>      store.clear().addCallback(function() {
<a name="line937"></a>        fail(&#39;clearing all with inactive transaction should have failed&#39;);
<a name="line938"></a>      }).addErrback(function(err) {
<a name="line939"></a>        assertEquals(expectedCode, err.code);
<a name="line940"></a>      });
<a name="line941"></a>
<a name="line942"></a>      index.get(&#39;anything&#39;).
<a name="line943"></a>          addCallback(function() {
<a name="line944"></a>            fail(&#39;getting from index with inactive transaction should have &#39; +
<a name="line945"></a>                &#39;failed&#39;);
<a name="line946"></a>          }).addErrback(function(err) {
<a name="line947"></a>            assertEquals(expectedCode, err.code);
<a name="line948"></a>          });
<a name="line949"></a>      index.getKey(&#39;anything&#39;).
<a name="line950"></a>          addCallback(function() {
<a name="line951"></a>            fail(&#39;getting key from index with inactive transaction &#39; +
<a name="line952"></a>                &#39;should have failed&#39;);
<a name="line953"></a>          }).addErrback(function(err) {
<a name="line954"></a>            assertEquals(expectedCode, err.code);
<a name="line955"></a>          });
<a name="line956"></a>      index.getAll(&#39;anything&#39;).
<a name="line957"></a>          addCallback(function() {
<a name="line958"></a>            fail(&#39;getting all from index with inactive transaction &#39; +
<a name="line959"></a>                &#39;should have failed&#39;);
<a name="line960"></a>          }).addErrback(function(err) {
<a name="line961"></a>            assertEquals(expectedCode, err.code);
<a name="line962"></a>          });
<a name="line963"></a>      index.getAllKeys(&#39;anything&#39;).
<a name="line964"></a>          addCallback(function() {
<a name="line965"></a>            fail(&#39;getting all keys from index with inactive transaction &#39; +
<a name="line966"></a>                &#39;should have failed&#39;);
<a name="line967"></a>          }).addErrback(function(err) {
<a name="line968"></a>            assertEquals(expectedCode, err.code);
<a name="line969"></a>          });
<a name="line970"></a>      closeAndContinue(db);
<a name="line971"></a>    });
<a name="line972"></a>  }).addErrback(failOnError);
<a name="line973"></a>}
<a name="line974"></a>
<a name="line975"></a>function testWrongTransactionMode() {
<a name="line976"></a>  if (!idbSupported) {
<a name="line977"></a>    return;
<a name="line978"></a>  }
<a name="line979"></a>
<a name="line980"></a>  asyncTestCase.waitForAsync(&#39;wrong transaction mode&#39;);
<a name="line981"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line982"></a>    var tx = db.createTransaction([&#39;store&#39;]);
<a name="line983"></a>    assertEquals(goog.db.Transaction.TransactionMode.READ_ONLY, tx.getMode());
<a name="line984"></a>    tx.objectStore(&#39;store&#39;).put(&#39;KABOOM!&#39;, &#39;anything&#39;).addCallback(function() {
<a name="line985"></a>      fail(&#39;putting should have failed&#39;);
<a name="line986"></a>    }).addErrback(function(err) {
<a name="line987"></a>      assertEquals(goog.db.Error.ErrorCode.READ_ONLY_ERR, err.code);
<a name="line988"></a>    });
<a name="line989"></a>    tx.objectStore(&#39;store&#39;).add(&#39;EXPLODE!&#39;, &#39;die&#39;).addCallback(function() {
<a name="line990"></a>      fail(&#39;adding should have failed&#39;);
<a name="line991"></a>    }).addErrback(function(err) {
<a name="line992"></a>      assertEquals(goog.db.Error.ErrorCode.READ_ONLY_ERR, err.code);
<a name="line993"></a>    });
<a name="line994"></a>    tx.objectStore(&#39;store&#39;).remove(&#39;no key&#39;, &#39;nothing&#39;).addCallback(function() {
<a name="line995"></a>      fail(&#39;deleting should have failed&#39;);
<a name="line996"></a>    }).addErrback(function(err) {
<a name="line997"></a>      assertEquals(goog.db.Error.ErrorCode.READ_ONLY_ERR, err.code);
<a name="line998"></a>    });
<a name="line999"></a>    closeAndContinue(db);
<a name="line1000"></a>  }).addErrback(failOnError);
<a name="line1001"></a>}
<a name="line1002"></a>
<a name="line1003"></a>function testManipulateIndexes() {
<a name="line1004"></a>  if (!idbSupported) {
<a name="line1005"></a>    return;
<a name="line1006"></a>  }
<a name="line1007"></a>
<a name="line1008"></a>  asyncTestCase.waitForAsync(&#39;index manipulation&#39;);
<a name="line1009"></a>  globalDb.branch().addCallback(function(db) {
<a name="line1010"></a>    return db.setVersion(&#39;testIndex&#39;).addCallback(function(tx) {
<a name="line1011"></a>      var store = db.createObjectStore(&#39;store&#39;);
<a name="line1012"></a>      store.createIndex(&#39;index&#39;, &#39;attr1&#39;);
<a name="line1013"></a>      store.createIndex(&#39;uniqueIndex&#39;, &#39;attr2&#39;, {unique: true});
<a name="line1014"></a>      store.createIndex(&#39;multirowIndex&#39;, &#39;attr3&#39;, {multirow: true});
<a name="line1015"></a>      return restartDatabase(db);
<a name="line1016"></a>    });
<a name="line1017"></a>  }).addCallback(function(db) {
<a name="line1018"></a>    return db.setVersion(&#39;testDelIndex&#39;).addCallback(function(tx) {
<a name="line1019"></a>      var store = tx.objectStore(&#39;store&#39;);
<a name="line1020"></a>      var index = store.getIndex(&#39;index&#39;);
<a name="line1021"></a>      var uniqueIndex = store.getIndex(&#39;uniqueIndex&#39;);
<a name="line1022"></a>      var multirowIndex = store.getIndex(&#39;multirowIndex&#39;);
<a name="line1023"></a>      try {
<a name="line1024"></a>        var dies = store.getIndex(&#39;diediedie&#39;);
<a name="line1025"></a>        fail(&#39;getting non-existent index should have failed&#39;);
<a name="line1026"></a>      } catch (err) {
<a name="line1027"></a>        assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
<a name="line1028"></a>      }
<a name="line1029"></a>      store.deleteIndex(&#39;index&#39;);
<a name="line1030"></a>      try {
<a name="line1031"></a>        store.deleteIndex(&#39;diediedie&#39;);
<a name="line1032"></a>        fail(&#39;deleting non-existent index should have failed&#39;);
<a name="line1033"></a>      } catch (err) {
<a name="line1034"></a>        assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
<a name="line1035"></a>      }
<a name="line1036"></a>      return restartDatabase(db);
<a name="line1037"></a>    });
<a name="line1038"></a>  }).addCallback(function(db) {
<a name="line1039"></a>    var tx = db.createTransaction([&#39;store&#39;]);
<a name="line1040"></a>    var store = tx.objectStore(&#39;store&#39;);
<a name="line1041"></a>    try {
<a name="line1042"></a>      var index = store.getIndex(&#39;index&#39;);
<a name="line1043"></a>      fail(&#39;getting deleted index should have failed&#39;);
<a name="line1044"></a>    } catch (err) {
<a name="line1045"></a>      assertEquals(goog.db.Error.ErrorCode.NOT_FOUND_ERR, err.code);
<a name="line1046"></a>    }
<a name="line1047"></a>    var uniqueIndex = store.getIndex(&#39;uniqueIndex&#39;);
<a name="line1048"></a>    var multirowIndex = store.getIndex(&#39;multirowIndex&#39;);
<a name="line1049"></a>  }).addCallback(closeAndContinue).addErrback(failOnError);
<a name="line1050"></a>}
<a name="line1051"></a>
<a name="line1052"></a>function testAddRecordWithIndex() {
<a name="line1053"></a>  if (!idbSupported) {
<a name="line1054"></a>    return;
<a name="line1055"></a>  }
<a name="line1056"></a>
<a name="line1057"></a>  asyncTestCase.waitForAsync(&#39;add record with index&#39;);
<a name="line1058"></a>  globalDb.branch().addCallback(addStoreWithIndex).addCallback(function(db) {
<a name="line1059"></a>    var COMPLETE = goog.db.Transaction.EventTypes.COMPLETE;
<a name="line1060"></a>    var addTx = db.createTransaction(
<a name="line1061"></a>        [&#39;store&#39;],
<a name="line1062"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line1063"></a>    var store = addTx.objectStore(&#39;store&#39;);
<a name="line1064"></a>    assertFalse(store.getIndex(&#39;index&#39;).isUnique());
<a name="line1065"></a>    assertEquals(&#39;value&#39;, store.getIndex(&#39;index&#39;).getKeyPath());
<a name="line1066"></a>    store.add({key: &#39;someKey&#39;, value: &#39;lookUpThis&#39;});
<a name="line1067"></a>
<a name="line1068"></a>    goog.events.listen(addTx, COMPLETE, function() {
<a name="line1069"></a>      var checkResultsTx = db.createTransaction([&#39;store&#39;]);
<a name="line1070"></a>      var index = checkResultsTx.objectStore(&#39;store&#39;).getIndex(&#39;index&#39;);
<a name="line1071"></a>      index.get(&#39;lookUpThis&#39;).addCallback(function(result) {
<a name="line1072"></a>        assertNotUndefined(result);
<a name="line1073"></a>        assertEquals(&#39;someKey&#39;, result.key);
<a name="line1074"></a>        assertEquals(&#39;lookUpThis&#39;, result.value);
<a name="line1075"></a>      });
<a name="line1076"></a>      index.getKey(&#39;lookUpThis&#39;).addCallback(function(result) {
<a name="line1077"></a>        assertNotUndefined(result);
<a name="line1078"></a>        assertEquals(&#39;someKey&#39;, result);
<a name="line1079"></a>      });
<a name="line1080"></a>      goog.events.listen(
<a name="line1081"></a>          checkResultsTx,
<a name="line1082"></a>          goog.db.Transaction.EventTypes.ERROR,
<a name="line1083"></a>          failOnErrorEvent);
<a name="line1084"></a>      goog.events.listen(checkResultsTx, COMPLETE, function() {
<a name="line1085"></a>        closeAndContinue(db);
<a name="line1086"></a>      });
<a name="line1087"></a>    });
<a name="line1088"></a>  }).addErrback(failOnError);
<a name="line1089"></a>}
<a name="line1090"></a>
<a name="line1091"></a>function testGetMultipleRecordsFromIndex() {
<a name="line1092"></a>  if (!idbSupported) {
<a name="line1093"></a>    return;
<a name="line1094"></a>  }
<a name="line1095"></a>
<a name="line1096"></a>  asyncTestCase.waitForAsync(&#39;get multiple records from index&#39;);
<a name="line1097"></a>  globalDb.branch().addCallback(addStoreWithIndex).addCallback(function(db) {
<a name="line1098"></a>    var COMPLETE = goog.db.Transaction.EventTypes.COMPLETE;
<a name="line1099"></a>    var addTx = db.createTransaction(
<a name="line1100"></a>        [&#39;store&#39;],
<a name="line1101"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line1102"></a>    addTx.objectStore(&#39;store&#39;).add({key: &#39;1&#39;, value: &#39;a&#39;});
<a name="line1103"></a>    addTx.objectStore(&#39;store&#39;).add({key: &#39;2&#39;, value: &#39;a&#39;});
<a name="line1104"></a>    addTx.objectStore(&#39;store&#39;).add({key: &#39;3&#39;, value: &#39;b&#39;});
<a name="line1105"></a>    // The following line breaks Chrome 14, but not Chrome 15:
<a name="line1106"></a>    // addTx.objectStore(&#39;store&#39;).add({key: &#39;4&#39;});
<a name="line1107"></a>    goog.events.listen(
<a name="line1108"></a>        addTx,
<a name="line1109"></a>        goog.db.Transaction.EventTypes.ERROR,
<a name="line1110"></a>        failOnErrorEvent);
<a name="line1111"></a>
<a name="line1112"></a>    goog.events.listen(addTx, COMPLETE, function() {
<a name="line1113"></a>      var checkResultsTx = db.createTransaction([&#39;store&#39;]);
<a name="line1114"></a>      var index = checkResultsTx.objectStore(&#39;store&#39;).getIndex(&#39;index&#39;);
<a name="line1115"></a>      index.getAll().addCallback(function(results) {
<a name="line1116"></a>        assertNotUndefined(results);
<a name="line1117"></a>        assertEquals(3, results.length);
<a name="line1118"></a>      });
<a name="line1119"></a>      index.getAll(&#39;a&#39;).addCallback(function(results) {
<a name="line1120"></a>        assertNotUndefined(results);
<a name="line1121"></a>        assertEquals(2, results.length);
<a name="line1122"></a>      });
<a name="line1123"></a>      index.getAllKeys().addCallback(function(results) {
<a name="line1124"></a>        assertNotUndefined(results);
<a name="line1125"></a>        assertEquals(3, results.length);
<a name="line1126"></a>      });
<a name="line1127"></a>      index.getAllKeys(&#39;b&#39;).addCallback(function(results) {
<a name="line1128"></a>        assertNotUndefined(results);
<a name="line1129"></a>        assertEquals(1, results.length);
<a name="line1130"></a>      });
<a name="line1131"></a>
<a name="line1132"></a>      goog.events.listen(
<a name="line1133"></a>          checkResultsTx,
<a name="line1134"></a>          goog.db.Transaction.EventTypes.ERROR,
<a name="line1135"></a>          failOnErrorEvent);
<a name="line1136"></a>      goog.events.listen(checkResultsTx, COMPLETE, function() {
<a name="line1137"></a>        closeAndContinue(db);
<a name="line1138"></a>      });
<a name="line1139"></a>    });
<a name="line1140"></a>  }).addErrback(failOnError);
<a name="line1141"></a>}
<a name="line1142"></a>
<a name="line1143"></a>function testUniqueIndex() {
<a name="line1144"></a>  if (!idbSupported) {
<a name="line1145"></a>    return;
<a name="line1146"></a>  }
<a name="line1147"></a>
<a name="line1148"></a>  asyncTestCase.waitForAsync(&#39;adding to unique index&#39;);
<a name="line1149"></a>  globalDb.branch().addCallback(function(db) {
<a name="line1150"></a>    return db.setVersion(&#39;test&#39;).addCallback(function(tx) {
<a name="line1151"></a>      var store = db.createObjectStore(&#39;store&#39;, {keyPath: &#39;key&#39;});
<a name="line1152"></a>      store.createIndex(&#39;index&#39;, &#39;value&#39;, {unique: true});
<a name="line1153"></a>      return restartDatabase(db);
<a name="line1154"></a>    });
<a name="line1155"></a>  }).addCallback(function(db) {
<a name="line1156"></a>    var tx = db.createTransaction(
<a name="line1157"></a>        [&#39;store&#39;],
<a name="line1158"></a>        goog.db.Transaction.TransactionMode.READ_WRITE);
<a name="line1159"></a>    assertTrue(tx.objectStore(&#39;store&#39;).getIndex(&#39;index&#39;).isUnique());
<a name="line1160"></a>    tx.objectStore(&#39;store&#39;).add({key: &#39;1&#39;, value: &#39;a&#39;});
<a name="line1161"></a>    tx.objectStore(&#39;store&#39;).add({key: &#39;2&#39;, value: &#39;a&#39;});
<a name="line1162"></a>    goog.events.listen(tx, goog.db.Transaction.EventTypes.ERROR, function(ev) {
<a name="line1163"></a>      // expected
<a name="line1164"></a>      assertTrue(
<a name="line1165"></a>          &#39;Expected DATA_ERR, CONSTRAINT_ERR or UNKNOWN_ERR, was &#39; +
<a name="line1166"></a>          ev.target.code,
<a name="line1167"></a>          // Chrome 15-21, 23+
<a name="line1168"></a>          ev.target.code == goog.db.Error.ErrorCode.CONSTRAINT_ERR ||
<a name="line1169"></a>          // Chrome 22
<a name="line1170"></a>          ev.target.code == goog.db.Error.ErrorCode.DATA_ERR ||
<a name="line1171"></a>          // Chrome 14
<a name="line1172"></a>          ev.target.code == goog.db.Error.ErrorCode.UNKNOWN_ERR);
<a name="line1173"></a>      closeAndContinue(db);
<a name="line1174"></a>    });
<a name="line1175"></a>  }).addErrback(failOnError);
<a name="line1176"></a>}
<a name="line1177"></a>
<a name="line1178"></a>function testDeleteDatabase() {
<a name="line1179"></a>  if (!idbSupported) {
<a name="line1180"></a>    return;
<a name="line1181"></a>  }
<a name="line1182"></a>
<a name="line1183"></a>  asyncTestCase.waitForAsync(&#39;deleting database&#39;);
<a name="line1184"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line1185"></a>    db.close();
<a name="line1186"></a>    return goog.db.deleteDatabase(dbName, function() {
<a name="line1187"></a>      fail(&#39;didn\&#39;t expect deleteDatabase to be blocked&#39;);
<a name="line1188"></a>    });
<a name="line1189"></a>  }).addCallback(function() {
<a name="line1190"></a>    return restartDatabase();
<a name="line1191"></a>  }).addCallback(assertStoreDoesntExist).addErrback(failOnError);
<a name="line1192"></a>}
<a name="line1193"></a>
<a name="line1194"></a>function testDeleteDatabaseIsBlocked() {
<a name="line1195"></a>  if (!idbSupported) {
<a name="line1196"></a>    return;
<a name="line1197"></a>  }
<a name="line1198"></a>
<a name="line1199"></a>  var wasBlocked = false;
<a name="line1200"></a>  asyncTestCase.waitForAsync(&#39;deleting database (blocked)&#39;);
<a name="line1201"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line1202"></a>    db.close();
<a name="line1203"></a>    // Get a fresh connection, without any events registered on globalDb.
<a name="line1204"></a>    return goog.db.openDatabase(dbName);
<a name="line1205"></a>  }).addCallback(function(db) {
<a name="line1206"></a>    return goog.db.deleteDatabase(dbName, function(ev) {
<a name="line1207"></a>      wasBlocked = true;
<a name="line1208"></a>      db.close();
<a name="line1209"></a>    });
<a name="line1210"></a>  }).addCallback(function() {
<a name="line1211"></a>    assertTrue(wasBlocked);
<a name="line1212"></a>    return restartDatabase();
<a name="line1213"></a>  }).addCallback(assertStoreDoesntExist).addErrback(failOnError);
<a name="line1214"></a>}
<a name="line1215"></a>
<a name="line1216"></a>function testBlockedDeleteDatabaseWithVersionChangeEvent() {
<a name="line1217"></a>  if (!idbSupported) {
<a name="line1218"></a>    return;
<a name="line1219"></a>  }
<a name="line1220"></a>
<a name="line1221"></a>  var gotVersionChange = false;
<a name="line1222"></a>  asyncTestCase.waitForAsync(&#39;deleting database (blocked)&#39;);
<a name="line1223"></a>  globalDb.branch().addCallback(addStore).addCallback(function(db) {
<a name="line1224"></a>    db.close();
<a name="line1225"></a>    // Get a fresh connection, without any events registered on globalDb.
<a name="line1226"></a>    return goog.db.openDatabase(dbName);
<a name="line1227"></a>  }).addCallback(function(db) {
<a name="line1228"></a>    goog.events.listen(
<a name="line1229"></a>        db, goog.db.IndexedDb.EventType.VERSION_CHANGE, function(ev) {
<a name="line1230"></a>          gotVersionChange = true;
<a name="line1231"></a>          db.close();
<a name="line1232"></a>        });
<a name="line1233"></a>    return goog.db.deleteDatabase(dbName);
<a name="line1234"></a>  }).addCallback(function() {
<a name="line1235"></a>    assertTrue(gotVersionChange);
<a name="line1236"></a>    return restartDatabase();
<a name="line1237"></a>  }).addCallback(assertStoreDoesntExist);
<a name="line1238"></a>}
</pre>


</body>
</html>
